<template>
  <div class="channel-edit">
    <van-cell class="cell" center :border="false">
      <div class="title" slot="title">我的频道</div>
      <van-button
        class="alter-btn"
        type="danger"
        plain
        round
        size="mini"
        @click="isShowIcon = !isShowIcon"
        >{{ isShowIcon ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="grid-item"
        :class="{ active: index === active }"
        v-for="(channel, index) in userChannels"
        :key="index"
        :icon="isShowIcon && index !== 0 ? 'close' : ''"
        :text="channel.name"
        @click="alterOrjump(index,channel)"
      />
    </van-grid>

    <van-cell class="cell" center :border="false">
      <div class="title" slot="title">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(item, index) in remommendChannels"
        :key="index"
        :text="item.name"
        @click="addUserC(item)"
      />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/channel'
import { mapState } from 'vuex'
export default {
  props: {
    userChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allchannels: [],
      isShowIcon: false
    }
  },
  computed: {
    ...mapState(['user']),
    remommendChannels () {
      return this.allchannels.filter((channel) => {
        return !this.userChannels.find((userChannel) => {
          return userChannel.id === channel.id
        })
      })
    }
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    async loadAllChannels () {
      const { data } = await getAllChannels()
      // console.log(data);
      this.allchannels = data.data.channels
    },
    // 添加我的频道
    async addUserC (item) {
      // console.log(item);
      if (this.isShowIcon) {
        this.userChannels.push(item)
        // 如果登录存储线上，没保存本地
        if (this.user) {
          // console.log(this.user);
          addUserChannel({
            channels: [{ id: item.id, seq: this.userChannels.length }]
          })
        } else {
          window.localStorage.setItem(
            'user-channels',
            JSON.stringify(this.userChannels)
          )
        }
      }
    },
    // 删除我的频道或者跳转频道,根据isShowIcon判断状态
    alterOrjump (index, channel) {
      if (this.isShowIcon) {
        // 编辑状态删除
        this.deleteChannel(index, channel)
      } else {
        // 非编辑状态切换
        this.switchChannel(index)
      }
    },
    async deleteChannel (index, channel) {
      // console.log("删除频道");
      if (index <= this.active) {
        this.$emit('delete', this.active - 1)
      }
      this.userChannels.splice(index, 1)
      // 删除持久化
      if (this.user) {
        await deleteUserChannel(channel.id)
      } else {
        window.localStorage.setItem(
          'user-channels',
          JSON.stringify(this.userChannels)
        )
      }
    },
    switchChannel (index) {
      this.$emit('close', index)
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  .cell {
    .alter-btn {
      padding: 0 20px;
    }
  }
  padding-top: 54px;
  .title {
    font-size: 16px;
    color: #333;
  }
  .grid-item {
    width: 80px;
    height: 43px;
    position: relative;
    /deep/ .van-grid-item__content {
      background-color: #f4f5f6;
      .van-grid-item__text {
        margin-top: 0;
      }
    }
    /deep/ .van-grid-item__icon {
      font-size: 20px;
      position: absolute;
      right: -7px;
      top: -7px;
      // background-color: black;
      color: #333;
    }
  }
  .active {
    /deep/ .van-grid-item__text {
      color: red;
    }
  }
}
</style>
